﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Azure.E2E.Common.Specialized;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Azure.E2E.Common.Tests
{
    [TestClass]
    [ExcludeFromCodeCoverage]
    public class TypeSegragatedLookupBaseTests
    {
        private sealed class Lookup1<TDifferentiator> : TypeSegregatedLookupBase<Lookup1<TDifferentiator>, string>
        {
            public static new bool TrySet<T>(string item)
            {
                return TypeSegregatedLookupBase<Lookup1<TDifferentiator>, string>.TrySet<T>(item);
            }
        }

        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
            Lookup1<string>.TrySet<int>("Hi");
            Lookup1<string>.TrySet<DateTime>("There");
        }

        [TestMethod]
        public void Test_TypeSegregatedLookupBase_Find_Existing()
        {
            Assert.AreEqual("Hi", Lookup1<string>.Find<int>());
            Assert.AreEqual("There", Lookup1<string>.Find<DateTime>());
        }

        [TestMethod]
        public void Test_TypeSegregatedLookupBase_Find_Existing_NonGeneric()
        {
            Assert.AreEqual("Hi", Lookup1<string>.Find(typeof(int)));
            Assert.AreEqual("There", Lookup1<string>.Find(typeof(DateTime)));
        }

        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void Test_TypeSegregatedLookupBase_Find_NonExistent_NonGeneric()
        {
            Lookup1<string>.Find(typeof(long));
        }

        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void Test_TypeSegregatedLookupBase_Find_NonExistent()
        {
            Lookup1<string>.Find<long>();
        }

        [TestMethod]
        public void Test_TypeSegregatedLookupBase_CanBeFound()
        {
            Assert.IsTrue(Lookup1<string>.CanBeFound());
            Assert.IsFalse(Lookup1<int>.CanBeFound());
        }

        [TestMethod]
        public void Test_TypeSegregatedLookupBase_TrySet()
        {
            Assert.IsTrue(Lookup1<string>.TrySet<TestMethodAttribute>("Weee"));
            Assert.IsFalse(Lookup1<string>.TrySet<int>("Chickens!"));
        }
    }
}
